package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.RoleService;
import com.cyz.common.bean.Role;
import com.cyz.common.bean.RoleMenu;
import com.cyz.common.dto.ChangeRoleStatusDto;
import com.cyz.common.dto.RoleDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.RoleMapper;
import com.cyz.common.service.RoleMenuService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.admin.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author 34928
* @description 针对表【role(角色信息表)】的数据库操作Service实现
* @createDate 2023-02-23 08:41:47
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public R queryRoles() {
        List<Role> list = list();
//        List<RoleVo> roleVos = BeanCopyUtil.copyBeanList(list, RoleVo.class);
        return R.success(list);
    }

    @Override
    public R queryRolesAndMenuIds() {
        List<Role> list = list();
        List<RoleVo> roleVos = BeanCopyUtil.copyBeanList(list, RoleVo.class);
        roleVos.forEach(item ->{
            item.setMenuIds(roleMenuService.getMenuIdsByRoleId(item.getId()));
        });
        return R.success(roleVos);
    }

    @Override
    public R updateRoleStatus(ChangeRoleStatusDto crd) {
        return null;
    }

    @Override
    public R updateRoles(RoleDto roleDto) {
        Role role = BeanCopyUtil.copyBean(roleDto, Role.class);
        boolean flag = updateById(role);
        roleMenuService.removeById(roleDto.getId());
        roleDto.getMenuIds().forEach(item -> {
            roleMenuService.save(new RoleMenu(roleDto.getId(),item));
        });
        if(flag){
            return queryRolesAndMenuIds();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R addRole(RoleDto roleDto) {
        Role role = BeanCopyUtil.copyBean(roleDto, Role.class);
        boolean flag = save(role);
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getRoleName,roleDto.getRoleName());
        Integer id = getOne(lambdaQueryWrapper).getId();
        roleDto.getMenuIds().forEach(item -> {
            roleMenuService.save(new RoleMenu(id,item));
        });

        if(flag){
            return queryRolesAndMenuIds();
        }
        return R.error(MyEnums.ADD_FAIL);
    }


    @Override
    public R deleteRoles(Integer[] ids) {
        boolean flag = removeByIds(Arrays.asList(ids));
        if(flag){
            return queryRolesAndMenuIds();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }

    @Override
    public List<String> queryRolesById(Integer userId) {
        return null;
    }
}




